package com.zsyh.jiujiu.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.SPUtils;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.SucessBean;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ModifyPassword extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tvyuan)
    TextView tvyuan;
    @BindView(R.id.yuanshipass)
    EditText yuanshipass;
    @BindView(R.id.tvxin)
    TextView tvxin;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.tvque)
    TextView tvque;
    @BindView(R.id.passagin)
    EditText passagin;
    @BindView(R.id.bntpassword)
    TextView bntpassword;

    @Override
    protected int initLayout() {
        return R.layout.activity_password;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }



    @OnClick(R.id.bntpassword)
    public void onViewClicked() {

        if(EmptyUtil.isEmpty(yuanshipass.getText().toString())){
            ToastUtils.showShort(thisActivity,"请输入原密码");
            return;
        }
        if(EmptyUtil.isEmpty(password.getText().toString())){
            ToastUtils.showShort(thisActivity,"请输入新密码");
            return;
        }
        if(EmptyUtil.isEmpty(passagin.getText().toString())){
            ToastUtils.showShort(thisActivity,"请再次确认密码");
            return;
        }

        if(password.getText().toString().equals(passagin.getText().toString())){
            xiugaimima((String) SPUtils.get(thisActivity, "userid", ""),yuanshipass.getText().toString(),passagin.getText().toString());
        }else{
            ToastUtils.showShort(thisActivity,"两次输入密码不一致");
        }

    }

    /**
     * 修改密码
     */
    public void xiugaimima(String userid,String oldPassword,String newPassword){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .updatePassword(userid,oldPassword,newPassword)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Gson gson=new Gson();
                        SucessBean sucessBean=gson.fromJson(JsonStr,SucessBean.class);
                        ToastUtils.showShort(thisActivity,sucessBean.getMsg());
                        thisActivity.finish();

                    }
                });
    }
}
